package cn.webwheel.tutorials.di;

import cn.webwheel.Action;
import cn.webwheel.utils.WebParam;

import javax.servlet.http.HttpServletRequest;

// index.jsp页面的动作类
public class PageIndex implements Action {

    private MessageService messageService;
    private HttpServletRequest request;

    private String param;

    // 通过WebWheel DI容器注入消息服务实例和当前的request对象
    public PageIndex(MessageService messageService, HttpServletRequest request) {
        this.messageService = messageService;
        this.request = request;
    }

    // 通过WebParam注释接口和DI容器注入web参数param
    @WebParam("param")
    public void setParam(String param) {
        this.param = param;
    }

    public Object execute() throws Exception {
        // 获得消息
        String msg = messageService.getMessage(param);
        // 在reqeust scope中存入属性msg
        request.setAttribute("msg", msg);
        // 通过返回null告诉WebWheelFilter未处理此次web请求，这样web请求就会继续通过WebWheelFilter而到达jsp页面
        return null;
    }
}
